/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet;

import DTO.playlist.Playlists;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import xmlutils.SearchPlaylistSAXHandler;
import xmlutils.XMLGenerator;

/**
 *
 * @author DinhNP
 */
public class managePlaylists extends HttpServlet {

    private final String errorPage = "errorUserPage.jsp";
    private final String xmlPath = "xml/Playlists.xml";
    private final String returnPage = "managePlaylist.jsp";

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            HttpSession session = request.getSession();
            if (session == null) {
                RequestDispatcher rd = request.getRequestDispatcher(errorPage);
                rd.forward(request, response);
            }
            String userName = session.getAttribute("USER").toString();
            String realPath = getServletContext().getRealPath("/");
            String filePath = realPath + xmlPath;

            if (userName != null) {
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sax = spf.newSAXParser();
                SearchPlaylistSAXHandler objHandler = new SearchPlaylistSAXHandler(userName);

                File file = new File(filePath);
                sax.parse(file, objHandler);

                String url = errorPage;
                Playlists userPlaylists = objHandler.getPlaylist();

                if (userPlaylists != null) {

                    JAXBContext ctx = JAXBContext.newInstance(userPlaylists.getClass());
                    Marshaller mar = ctx.createMarshaller();
                    mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
                    mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

                    response.setContentType("text/xml");
                    response.setCharacterEncoding("UTF-8");
                    mar.marshal(userPlaylists, out);
                }
            }
        } catch (JAXBException ex) {
            Logger.getLogger(managePlaylists.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(managePlaylists.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(managePlaylists.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
